package com.hpin.mlxteacher.child;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.ChooseAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.People;
import com.hpin.mlxteacher.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 考勤页面
 * Created by Aaron on 2015/7/28.
 */
public class AttendanceActivity extends BaseActivity {
    //红线
    private TextView tv_red_line;
    //屏幕的宽度
    private int screenwidth;
    //当前显示的 是上午还是中午还是下午 的 角标
    private int currentMessageIndex;
    private GridView gv_child;
    private ChooseAdapter amAdapter;
    private  ChooseAdapter noonAdapter;
    private  ChooseAdapter pmAdapter;
    private TextView tv_am;
    private TextView tv_noon;
    private TextView tv_pm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_attendance);

        initWidget();
    }

    private void initWidget() {
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.attendance));
        tv_right.setText(getResources().getString(R.string.history));

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        tv_am = (TextView) findViewById(R.id.tv_choose_left);
        tv_noon = (TextView) findViewById(R.id.tv_choose_middle);
        tv_pm = (TextView) findViewById(R.id.tv_choose_right);

        tv_am.setText(getResources().getString(R.string.am));
        tv_noon.setText(getResources().getString(R.string.noon));
        tv_pm.setText(getResources().getString(R.string.pm));

        gv_child = (GridView) findViewById(R.id.gv_child);

        tv_red_line = (TextView) findViewById(R.id.tv_red_line);

        tv_am.setOnClickListener(this);
        tv_noon.setOnClickListener(this);
        tv_pm.setOnClickListener(this);

        screenwidth = CommonUtils.getScreenSize(this)[0];

        //默认显示上午
        currentMessageIndex = 0;

        if (amAdapter==null) {
            List<People> peoples = new ArrayList<People>();
            People p1 = new People();
            p1.name = "张大大";
            People p2 = new People();
            p2.name = "张小小";
            People p3 = new People();
            p3.name = "张大小";
            People p4 = new People();
            p4.name = "张小大";
            People p5 = new People();
            p5.name = "张轩轩";
            People p6 = new People();
            p6.name = "李大大";
            peoples.add(p1);
            peoples.add(p2);
            peoples.add(p3);
            peoples.add(p4);
            peoples.add(p5);
            peoples.add(p6);
            amAdapter = new ChooseAdapter(this, peoples);
            gv_child.setAdapter(amAdapter);
        }


//        gv_child.setAdapter();

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right://历史

                Intent hIntent = new Intent(this, HistoryActivity.class);
                startActivity(hIntent);

                break;

            case R.id.tv_choose_left://上午
                changeTextColor2Gray();
                tv_am.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 0);

                if (amAdapter==null) {
                    List<People> peoples = new ArrayList<People>();
                    People p1 = new People();
                    p1.name = "张大大";
                    People p2 = new People();
                    p2.name = "张小小";
                    People p3 = new People();
                    p3.name = "张大小";
                    People p4 = new People();
                    p4.name = "张小大";
                    People p5 = new People();
                    p5.name = "张轩轩";
                    People p6 = new People();
                    p6.name = "李大大";
                    peoples.add(p1);
                    peoples.add(p2);
                    peoples.add(p3);
                    peoples.add(p4);
                    peoples.add(p5);
                    peoples.add(p6);
                    amAdapter = new ChooseAdapter(this, peoples);
                }
                gv_child.setAdapter(amAdapter);

                break;

            case R.id.tv_choose_middle://中午
                changeTextColor2Gray();
                tv_noon.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 1);

                if (noonAdapter==null) {
                    List<People> peoples = new ArrayList<People>();
                    People p1 = new People();
                    p1.name = "张大大";
                    People p2 = new People();
                    p2.name = "张小小";
                    People p3 = new People();
                    p3.name = "张大小";
                    People p4 = new People();
                    p4.name = "张小大";
                    People p5 = new People();
                    p5.name = "张轩轩";
                    peoples.add(p1);
                    peoples.add(p2);
                    peoples.add(p3);
                    peoples.add(p4);
                    peoples.add(p5);
                    noonAdapter = new ChooseAdapter(this, peoples);

                }
                gv_child.setAdapter(noonAdapter);

                break;

            case R.id.tv_choose_right://下午
                changeTextColor2Gray();
                tv_pm.setTextColor(getResources().getColor(R.color.red));
                changeRedLine(currentMessageIndex, 2);

                if (pmAdapter==null) {
                    List<People> peoples = new ArrayList<People>();
                    People p1 = new People();
                    p1.name = "张大大";
                    People p2 = new People();
                    p2.name = "张小小";
                    People p3 = new People();
                    p3.name = "张大小";
                    People p4 = new People();
                    p4.name = "张小大";
                    peoples.add(p1);
                    peoples.add(p2);
                    peoples.add(p3);
                    peoples.add(p4);

                    pmAdapter = new ChooseAdapter(this, peoples);

                }
                gv_child.setAdapter(pmAdapter);

                break;
        }
    }

    /**
     * 改变红线的位置
     * @param currentIndex 当前红线在哪个位置
     * @param toIndex 到哪个位置
     */
    private void changeRedLine(int currentIndex, int toIndex) {

        ObjectAnimator anim = null;
        //获取当前控件的x距离
        float  curTranslationX = tv_red_line.getTranslationX();

//        LogUtil.e("TAG", "curTranslationX==>" + curTranslationX);

        switch (currentIndex) {

            case 0:

                //当前在第一位置
                switch (toIndex) {

                    case 1 :

                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3);
                        currentMessageIndex = 1;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3 * 2);
                        currentMessageIndex=2;
                        break;

                }

                break;

            case 1:

                //当前在第二位置
                switch (toIndex) {

                    case 0 :

                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex=0;
                        break;

                    case 2:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3*2);
                        currentMessageIndex=2;
                        break;

                }

                break;

            case 2:

                //当前在第三位置
                switch (toIndex) {

                    case 0 :
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, 0);
                        currentMessageIndex=0;
                        break;

                    case 1:
                        anim = ObjectAnimator.ofFloat(tv_red_line, "translationX", curTranslationX, screenwidth/3);
                        currentMessageIndex=1;
                        break;
                }

                break;

        }

        if (anim!=null) {
            //设置移动时间0.5秒s
            anim.setDuration(500);
            anim.start();
        }

    }
    /**
     * 把字体颜色都变灰色
     */
    private void changeTextColor2Gray() {

        tv_am.setTextColor(getResources().getColor(R.color.black));
        tv_noon.setTextColor(getResources().getColor(R.color.black));
        tv_pm.setTextColor(getResources().getColor(R.color.black));

    }
}
